package com.p7.notepad.app;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ItemDecoration;
import android.support.v7.widget.RecyclerView.LayoutManager;
import com.tools.s7.recycler.BaseRecyclerAdapter;
import com.tools.s7.recycler.listener.Recycler.OnCheckedChangeListener;
import com.tools.s7.recycler.listener.Recycler.OnClickListener;
import com.tools.s7.recycler.listener.Recycler.OnItemClickListener;
import com.tools.s7.recycler.listener.Recycler.OnItemLongClickListener;
import com.tools.s7.recycler.listener.Recycler.OnLoadingListener;
import java.util.List;

public class LoadMoreHelper<T> {
    private boolean hasInitAdapter = false;
    private int page = 1;
    private int pageSize = 100;
    private BaseRecyclerAdapter<T> mAdapter;
    private SwipeRefreshLayout mSwipeRefresh;
    private RecyclerView mRecycler;
    private OnCheckedChangeListener<T> mCheckedChange;
    private OnClickListener<T> mClick;
    private OnItemClickListener<T> mItemClick;
    private OnItemLongClickListener<T> mItemLongClick;
    private LoadDataListener mLoadData;

    private boolean isLoad = false;

    public LoadMoreHelper() {
    }

    protected void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    protected void setAdapter(BaseRecyclerAdapter<T> mAdapter) {
        this.mAdapter = mAdapter;
    }

    protected void setSwipeRefresh(SwipeRefreshLayout mSwipeRefresh) {
        this.mSwipeRefresh = mSwipeRefresh;
    }

    protected void setRecycler(RecyclerView mRecycler) {
        this.mRecycler = mRecycler;
    }

    public void setCheckedChange(OnCheckedChangeListener<T> mCheckedChange) {
        this.mCheckedChange = mCheckedChange;
    }

    protected void setClick(OnClickListener<T> mClick) {
        this.mClick = mClick;
    }

    protected void setItemClick(OnItemClickListener<T> mItemClick) {
        this.mItemClick = mItemClick;
    }

    protected void setItemLongClick(OnItemLongClickListener<T> mItemLongClick) {
        this.mItemLongClick = mItemLongClick;
    }

    protected void setLoadData(LoadDataListener mLoadData) {
        this.mLoadData = mLoadData;
    }

    protected void initAdapter() {
        if (mCheckedChange != null) {
            mAdapter.setCheckedChange(mCheckedChange);
        }

        if (mClick != null) {
            mAdapter.setClick(mClick);
        }

        if (mItemClick != null) {
            mAdapter.setItemClick(mItemClick);
        }

        if (mItemLongClick != null) {
            mAdapter.setItemLongClick(mItemLongClick);
        }

        if (mSwipeRefresh != null && mLoadData != null) {
            mSwipeRefresh.setOnRefreshListener(new OnRefreshListener() {
                public void onRefresh() {
                    page = 1;
                    mLoadData.onLoadData(page);
                    isLoad = false;
                    isLoad();
                }
            });
        }

        mAdapter.setPageSize(pageSize);
        if (mLoadData != null) {
            mAdapter.setLoadingListener(new OnLoadingListener() {
                public void onReload() {
                    setSwipeRefresh(true);
                    page = 1;
                    mLoadData.onLoadData(page);
                    isLoad = false;
                    isLoad();
                }

                public void onLoading() {
                    isLoad = false;
                    mLoadData.onLoadData(page);
                    isLoad();
                }
            });
        }

        mRecycler.setAdapter(mAdapter);
        hasInitAdapter = true;
    }

    public void loadFirstPage() {
        page = 1;
        if (!hasInitAdapter) {
            initAdapter();
        }

        if (mLoadData != null) {
            isLoad = false;
            setSwipeRefresh(true);
            mLoadData.onLoadData(page);
        }
    }

    public void isLoad() {
        if (isLoad) {
            mSwipeRefresh.setEnabled(true);
        } else {
            mSwipeRefresh.setEnabled(false);
        }
    }

    public void notifyAdapter(List<T> datas) {
        if (page == 1) {
            setSwipeRefresh(false);
            mAdapter.onRefresh(datas);
        } else {
            mAdapter.addData(datas);
        }
        isLoad = true;
        isLoad();
        ++page;
    }

    public void notifyAdapter() {
        mAdapter.notifyDataSetChanged();
    }

    public void notifyNull() {
        isLoad = true;
        isLoad();
        if (page == 1) {
            setSwipeRefresh(false);
            mAdapter.setNull(true);
        } else {
            mAdapter.addData((List)null);
        }
    }

    public void notifyError() {
        isLoad = true;
        isLoad();
        if (page == 1) {
            setSwipeRefresh(false);
            mAdapter.setError(true);
        } else {
            mAdapter.setLoadError(true);
        }

    }

    public void notifyRefresh(List<T> datas) {
        mAdapter.onRefresh(datas);
    }

    public void notifyReplace(int position, T data) {
        mAdapter.onReplace(position, data);
    }

    public void notifyRemove(int position) {
        mAdapter.onRemove(position);
    }

    public List<T> getData() {
        return mAdapter.getDatas();
    }

    private void setSwipeRefresh(boolean flag) {
        if (mSwipeRefresh != null) {
            if (flag) {
                if (!mSwipeRefresh.isRefreshing()) {
                    mSwipeRefresh.setRefreshing(true);
                }
            } else if (mSwipeRefresh.isRefreshing()) {
                mSwipeRefresh.setRefreshing(false);
            }
        }

    }

    public interface LoadDataListener {
        void onLoadData(int var1);
    }

    public static class Builder<T> {
        private int pageSize = 100;
        private BaseRecyclerAdapter<T> mAdapter;
        private SwipeRefreshLayout mSwipeRefresh;
        private RecyclerView mRecycler;
        private OnCheckedChangeListener<T> mCheckedChange;
        private OnClickListener<T> mClick;
        private OnItemClickListener<T> mItemClick;
        private OnItemLongClickListener<T> mItemLongClick;
        private LoadDataListener mLoadData;

        public Builder() {
        }

        public Builder setPageSize(int pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Builder setAdapter(BaseRecyclerAdapter<T> mAdapter) {
            this.mAdapter = mAdapter;
            return this;
        }

        public Builder setAdapter(BaseRecyclerAdapter<T> mAdapter, boolean isFooter) {
            this.mAdapter = mAdapter;
            this.mAdapter.setFooter(isFooter);
            return this;
        }

        public Builder setAdapter(BaseRecyclerAdapter<T> mAdapter, boolean isLoad, boolean isFooter) {
            this.mAdapter = mAdapter;
            this.mAdapter.setLoad(isLoad).setFooter(isFooter);
            return this;
        }

        public Builder setSwipeRefresh(SwipeRefreshLayout mSwipeRefresh) {
            this.mSwipeRefresh = mSwipeRefresh;
            this.mSwipeRefresh.setColorSchemeColors(new int[]{-65536, -256, -16776961, -16711936});
            return this;
        }

        public Builder setSwipeRefresh(SwipeRefreshLayout mSwipeRefresh, int... colors) {
            this.mSwipeRefresh = mSwipeRefresh;
            this.mSwipeRefresh.setColorSchemeColors(colors);
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, LayoutManager layoutManager) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            return this;
        }

        public Builder setRecycler(RecyclerView mRecycler, LayoutManager layoutManager, ItemDecoration itemDecoration) {
            this.mRecycler = mRecycler;
            this.mRecycler.setLayoutManager(layoutManager);
            this.mRecycler.addItemDecoration(itemDecoration);
            return this;
        }

        public Builder setCheckedChange(OnCheckedChangeListener<T> mCheckedChange) {
            this.mCheckedChange = mCheckedChange;
            return this;
        }

        public Builder setClick(OnClickListener<T> mClick) {
            this.mClick = mClick;
            return this;
        }

        public Builder setItemClick(OnItemClickListener<T> mItemClick) {
            this.mItemClick = mItemClick;
            return this;
        }

        public Builder setItemLongClick(OnItemLongClickListener<T> mItemLongClick) {
            this.mItemLongClick = mItemLongClick;
            return this;
        }

        public Builder setLoadData(LoadDataListener mLoadData) {
            this.mLoadData = mLoadData;
            return this;
        }

        public <T> LoadMoreHelper<T> build() {
            LoadMoreHelper mHelper = new LoadMoreHelper();
            mHelper.setAdapter(mAdapter);
            mHelper.setSwipeRefresh(mSwipeRefresh);
            mHelper.setRecycler(mRecycler);
            mHelper.setCheckedChange(mCheckedChange);
            mHelper.setClick(mClick);
            mHelper.setItemClick(mItemClick);
            mHelper.setItemLongClick(mItemLongClick);
            mHelper.setPageSize(pageSize);
            mHelper.setLoadData(mLoadData);
            return mHelper;
        }
    }
}
